/**
 * 商品编辑
 * */


//获取分类
function getCateList(){
    $.ajax({
        type: apis.jfGcateSel.method,
        url: apis.jfGcateSel.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<option value="' + d[i].id + '">';
                h += d[i].title;
                h += '</option>';
            }
            $('#cateid').html(h);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//显示图片
function showImg(e){
    if (e.type == 1) {
        showBigImg({domain: e.domain, img: e.img, w: e.w, h: e.h});
    } else {
        showMoreImg({domain: e.domain, img: e.img, w: e.w, h: e.h});
    }
}


//显示主图
function showBigImg(e){
    console.log(e);
    var selid = 'big';
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    $('#' + selid + 'Show').html(h).width(e.w).height(e.h);
    formData.bigImg = e.img;
}


//显示多图
function showMoreImg(e){
    var selid = 'more';
    var h = '', i;
    formData.moreImgs.push({img: e.img, w: e.w, h: e.h});
    var moreImgs = formData.moreImgs;
    var outW = 0;
    for (i = 0; i < moreImgs.length; i ++) {
        h += '<div style="width: ' + (moreImgs[i].w*1+2)+ 'px; height: ' + moreImgs[i].h + 'px; overflow: hidden;">';
        h += '<img src="' + e.domain + moreImgs[i].img + '" style="width: ' + moreImgs[i].w + 'px; height: ' + moreImgs[i].h + 'px; border: 1px solid #000;"/>';
        h += '<a href="javascript:;" style="padding: 10px 20px; color: #fff; background: red; font-size: 12px; opacity: 0.7; position: relative; top: -30px; left: 0;" onclick="imgDel(' + i + ');">点击删除</a>';
        h += '</div>';
        if (outW < moreImgs[i].w) { outW = moreImgs[i].w; }
    }
    var hh = ''; //上传按钮复位
    hh += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">上传图片</div>';
    hh += '<input type="file" id="' + selid + 'Upload" style="width: 150px; height: 30px; opacity: 0; cursor: pointer; position: relative; top: -25px; left: -20px;" onchange="uploadImage(2)"/>';
    $('#' + selid + 'Div').html(hh);
    $('#' + selid + 'Out').width(outW);
    $('#' + selid + 'Show').html(h).width(e.w*1+2).height(e.h*1+2);
    $('#' + selid + 'Total').html('当前' + formData.moreImgs.length + '张');
}


//移除图片
function imgDel(v){
    var imgs = formData.moreImgs;
    var arys = [], i;
    for (i = 0; i < imgs.length; i ++) {
        if (i != v) {
            arys.push(imgs[i]);
        }
    }
    formData.moreImgs = arys;
    var outW = 0, h = '', mw = 0, mh = 0;
    for (i = 0; i < arys.length; i ++) {
        h += '<div style="width: ' + (arys[i].w*1+2)+ 'px; height: ' + arys[i].h + 'px; overflow: hidden;">';
        h += '<img src="' + arys[i].img + '" style="width: ' + arys[i].w + 'px; height: ' + arys[i].h + 'px; border: 1px solid #000;"/>';
        h += '<a href="javascript:;" style="padding: 10px 20px; color: #fff; background: red; font-size: 12px; opacity: 0.7; position: relative; top: -30px; left: 0;" onclick="imgDel(' + i + ');">点击删除</a>';
        h += '</div>';
        if (outW < arys[i].w) { outW = arys[i].w; }
        mw += arys[i].w * 1;
        mh += arys[i].h * 1;
    }
    mw = mw==0 ? 500 : mw;
    mh = mh==0 ? 300 : mh;
    $('#moreOut').width(outW);
    $('#moreShow').html(h).width(mw*1+2).height(mh*1+2);
}


//SKU的HTML
function skuHtml(){
    var h = '', i;
    for (i = 0; i < skuArys.length; i ++) {
        h += '<div class="skuOne">';
        //h += '<input type="hidden" id="sku' + i + '" />';
        h += '<div class="row cl">';
        h += '<label class="form-label col-xs-4 col-sm-2">规格名称：</label>';
        h += '<div class="formControls col-xs-8 col-sm-9">';
        h += '<input type="text" class="input-text" value="' + skuArys[i].title + '" disabled/>';
        h += '</div>';
        h += '</div>';
        h += '<div class="row cl">';
        h += '<label class="form-label col-xs-4 col-sm-2">积分数：</label>';
        h += '<div class="formControls col-xs-8 col-sm-9">';
        h += '<input type="text" class="input-text" value="' + skuArys[i].price + '" disabled/>';
        h += '</div>';
        h += '</div>';
        h += '<div class="row cl">';
        h += '<label class="form-label col-xs-4 col-sm-2">划线价(元)：</label>';
        h += '<div class="formControls col-xs-8 col-sm-9">';
        h += '<input type="text" class="input-text" value="' + skuArys[i].price_line + '" disabled/>';
        h += '</div>';
        h += '</div>';
        h += '<div class="row cl">';
        h += '<label class="form-label col-xs-4 col-sm-2">库存量：</label>';
        h += '<div class="formControls col-xs-8 col-sm-9">';
        h += '<input type="text" class="input-text" value="' + skuArys[i].stock + '" disabled/>';
        h += '</div>';
        h += '</div>';
        h += '<div class="row cl">';
        h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
        h += '<button class="btn btn-primary radius" type="button" style="background: red; border: 0;" onclick="skuDel(' + i + ');">';
        h += '删除该SKU';
        h += '</button>';
        h += '</div>';
        h += '</div>';
        h += '</div>';
        h += '';
    }
    $('#skuGroup').html(h);
}


//SKU复位
function skuInit(){
    skuData = {
        title: '',
        price: 0,
        price_line: 0,
        stock: 0,
    };
    $('#sku_name')[0].value = '';
    $('#sku_price')[0].value = 0;
    $('#sku_price_line')[0].value = 0;
    $('#sku_stock')[0].value = 0;
}


//添加SKU
function skuAdd(){
    console.log(skuData);
    if (!skuData.title) {
        layer.msg('规格名称必须的',{icon: 0,time: 1000});
        return
    }
    if (skuData.price == '') {
        layer.msg('积分数必须的',{icon: 0,time: 1000});
        return
    }
    if (skuData.price_line == '') {
        layer.msg('划线价必须的',{icon: 0,time: 1000});
        return
    }
    if (skuData.stock == '') {
        layer.msg('库存必须的',{icon: 0,time: 1000});
        return
    }
    skuArys.push(skuData);
    skuHtml();
    skuInit();
}


//移除SKU
function skuDel(v){
    var arys = [], i;
    for (i = 0; i < skuArys.length; i ++) {
        if (i != v) {
            arys.push(skuArys[i]);
        }
    }
    skuArys = arys;
    skuHtml();
}


//保存商品
function gSave(){
    if (!formData.title) {
        layer.msg('商品名称必须的',{icon: 0,time: 1000});
        return
    }
    if (!formData.cateid) {
        layer.msg('分类必须的',{icon: 0,time: 1000});
        return
    }
    if (formData.bigImg == '') {
        layer.msg('主图必须的',{icon: 0,time: 1000});
        return
    }
    if (formData.moreImgs.length == 0) {
        layer.msg('详情图必须的',{icon: 0,time: 1000});
        return
    }
    if (skuArys.length == 0) {
        layer.msg('SKU规格必须的',{icon: 0,time: 1000});
        return
    }
    formData.skulist = skuArys;
    formData.detail = ue.getContent();
    //console.log(formData); return
    $.ajax({
        type: apis.jfGadd.method,
        url: apis.jfGadd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '商品添加成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
